package cycles.util.jdbc;

import java.lang.reflect.Field;
import java.util.Map;

enum SQLCommand {
	INSERT {
		@Override
		public String buildSql(String tablename, Map<Field, String> obj2tbl,
				Field[] destfields, Field[] whereFields, Field orderby,
				boolean asc, int[] limit) {
			StringBuilder sb = new StringBuilder("insert into " + tablename
					+ " (");
			for (Field field : destfields) {
				sb.append(obj2tbl.get(field));
				sb.append(",");
			}
			sb.setCharAt(sb.length() - 1, ')');// 将最后多余的逗号替换为后括号
			sb.append(" values (");
			for (int i = 0; i < destfields.length - 1; i++) {
				sb.append("?,");
			}
			sb.append("?)");
			return sb.toString();
		}
	}, //
	SELECT {

		@Override
		public String buildSql(String tablename, Map<Field, String> obj2tbl,
				Field[] destFields, Field[] whereFields, Field orderby,
				boolean asc, int[] limit) {
			StringBuilder sb = new StringBuilder("select ");
			if (destFields != null && destFields.length > 0) {// 有限选几个字段
				for (Field fld : destFields) {
					sb.append(obj2tbl.get(fld));
					sb.append(",");
				}
				sb.append(" from " + tablename);
			} else {
				sb.append("* from " + tablename);
			}
			return SQLCommand.concatCondition(sb, obj2tbl, whereFields,
					orderby, asc, limit).toString();
		}

	}, //
	UPDATE {
		@Override
		public String buildSql(String tablename, Map<Field, String> obj2tbl,
				Field[] destFields, Field[] whereFields, Field orderby,
				boolean asc, int[] limit) {
			StringBuilder sb = new StringBuilder("update " + tablename
					+ " set ");
			// update语句不能没有目标字段,这里没有做判断，应用自己负责
			for (int i = 0; i < destFields.length - 1; i++) {
				sb.append(obj2tbl.get(destFields[i]));
				sb.append("=?,");
			}
			sb.append(obj2tbl.get(destFields[destFields.length - 1]));
			sb.append("=?");
			return SQLCommand.concatCondition(sb, obj2tbl, whereFields,
					orderby, asc, limit).toString();
		}

	}, //
	DELETE {
		@Override
		public String buildSql(String tablename, Map<Field, String> obj2tbl,
				Field[] destFields, Field[] whereFields, Field orderby,
				boolean asc, int[] limit) {
			return SQLCommand.concatCondition(
					new StringBuilder("delete from " + tablename), obj2tbl,
					whereFields, orderby, asc, limit).toString();
		}

	};

	public String buildSql(String tablename, Map<Field, String> obj2tbl,
			Field[] destFields, Field[] whereFields, Field orderby,
			boolean asc, int[] limit) {
		return null;
	}

	private static final StringBuilder concatCondition(StringBuilder sb,
			Map<Field, String> obj2tbl, Field[] whereFields, Field orderby,
			boolean asc, int[] limit) {
		if (whereFields != null && whereFields.length > 0) {
			sb.append(" where ");
			for (int i = 0; i < whereFields.length - 1; i++) {
				sb.append(obj2tbl.get(whereFields[i]));
				sb.append("=? and ");
			}
			sb.append(obj2tbl.get(whereFields[whereFields.length - 1]));
			sb.append("=?");

		}

		if (orderby != null) {
			sb.append(" order by " + obj2tbl.get(orderby) + " "
					+ (asc ? "asc" : "desc"));
		}
		if (limit != null) {
			if (limit.length == 1) {
				sb.append(" limit " + limit[0] + "," + limit[1]);
			} else {
				sb.append(" limit " + limit[0]);
			}
		}
		return sb;
	}

	public static void main(String[] args) {

		for (SQLCommand cmd : SQLCommand.values()) {
			System.out.println(cmd.name());
		}
	}
}
